//
//  CartCell.m
//  sensen_ios
//
//  Created by pro on 16/2/26.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "CartCell.h"


#define cell_width self.contentView.width
#define cell_height self.contentView.height

@interface CartCell ()



@property (nonatomic, strong) UILabel *priceLabel; // 商品价格



@end

@implementation CartCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        // 设置单元格不可点击
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.contentView.width = SCREEN_WIDTH;
        self.contentView.height = 80;
        [self _initViews];
    }
    
    return self;
}


#pragma mark -- 创建视图 --
- (void)_initViews {
    for (UIView *view in self.contentView.subviews) {
        [view removeFromSuperview];
    }

    CGFloat x, y, width, height;
    
    // 选中按钮
    x = 0;
    width = 40;
    height = cell_height;
    _selectButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _selectButton.frame = CGRectMake(x, y, width, height);
    [_selectButton setImage:[UIImage imageNamed:@"购物车_未选中"] forState:UIControlStateNormal];
    [_selectButton setImage:[UIImage imageNamed:@"购物车_选中"] forState:UIControlStateSelected];
    _selectButton.imageEdgeInsets = UIEdgeInsetsMake(height - 30, 10, height - 30, 10);
    [_selectButton addTarget:self.viewController action:@selector(selectAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_selectButton];
    _selectButton.tag = 10;
    _selectButton.center = CGPointMake(_selectButton.center.x, cell_height / 2);
    
    // 商品图片
    x = _selectButton.right + 5;
    y = 10;
    width = cell_height - 2 * y;
    height = width;
    _iconView = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    _iconView.image = [UIImage imageWithColor:BLUE_COLOR];
    [self.contentView addSubview:_iconView];
    _iconView.center = CGPointMake(_iconView.center.x, cell_height / 2);
    
    // 商品名称
    x = _iconView.right + 5;
    y = _iconView.top - 5;
    width = cell_width - x - 5;
    height = 30;
    _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
    _nameLabel.font = FONT(12);
    _nameLabel.numberOfLines = 2;
    _nameLabel.text = @"森森三合一潜水泵鱼缸过滤器鱼缸水泵增氧过滤水泵水族箱鱼缸潜水泵";
    _nameLabel.textColor = [UIColor darkGrayColor];
    _nameLabel.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:_nameLabel];
    
    // 商品规格
    x = _nameLabel.left;
    y = _nameLabel.bottom;
    height = 15;
    _skuLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
    _skuLabel.font = FONT(12);
    _skuLabel.text = @"颜色分类:黑色;规格:大";
    _skuLabel.textColor = [UIColor lightGrayColor];
    _skuLabel.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:_skuLabel];
    
    // 数量修改视图
    height = 25;
    width = height * 10 / 3;
    y = _skuLabel.bottom;
    x = cell_width - 5 - width;
    _amountModifyView = [[AmountModifyView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    [self.contentView addSubview:_amountModifyView];
    
    // 价格
    x = _skuLabel.left;
    y = _skuLabel.bottom;
    width = _amountModifyView.left - x - 5;
    _priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
    _priceLabel.font = FONT(15);
    _priceLabel.text = @"￥2819";
    _priceLabel.textColor = SS_NAVI_ORANGE_COLOR;
    _priceLabel.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:_priceLabel];
    
    

}



#pragma mark - 点击事件
//- (void)selectAction:(UIButton *)button {
//    button.selected = !button.selected;
//}



#pragma mark - 重写set
- (void)setModel:(CartProductModel *)model {
    _model = model;
    
    [self _layoutViews];
}
// 重新布局视图
- (void)_layoutViews {
 
    // 商品图标
    [_iconView setImageURLStr:_model.icon_url placeholder:DEFAULT_IMAGE];
    // 名称
    _nameLabel.text = [NSString stringWithFormat:@"%@", _model.name];
    // 规格
    _skuLabel.text = [NSString stringWithFormat:@"%@", _model.sku_desc];
    // 价格
    _priceLabel.text = [NSString stringWithFormat:@"￥%@", _model.price];
    // 数量
    _amountModifyView.amount = [_model.count integerValue];
}
@end
